/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.linker.web;

import java.util.Observable;
import java.util.Observer;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.core.web.event.MenuCreatingEventHandler;
import org.fyx.core.web.event.RoleCreatingEventHandler;
import org.fyx.linker.LinkerRoles;
import org.fyx.secure.entity.SecureRole;
import org.fyx.secure.ejb.SecureLocal;
import org.primefaces.component.menuitem.MenuItem;

/**
 * Observer pro naslouchani na vnitrni udalosti.
 * Zoodpovida za spravne provedeni akci na urcite udalosti.
 * 
 * @author xljan
 */
@Stateless
@LocalBean
public class LinkerObserver implements Observer {

    @EJB
    private SecureLocal secure;
    @EJB
    private LinkerLocal linker;
    @EJB
    private MulaLocal mula;

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof MenuCreatingEventHandler) {
            menuCreate();
        } else if (o instanceof RoleCreatingEventHandler) {
            roleCreate();
        }
    }

    private void menuCreate() {
        MenuItem item = new MenuItem();
        item.setValue(mula.get("linker_category"));
        item.setIcon("ui-icon ui-icon-shuffle");
        item.setUrl(linker.getLink("linker/category/edit.xhtml"));
        item.setId("linker_category");
        linker.addMenuItem(item, LinkerRoles.CATEGORY_MANAGER);
    }

    private void roleCreate() {
        SecureRole role = new SecureRole(LinkerRoles.CATEGORY_MANAGER);
        role.setCategory("Category");
        role.setTitle("Category manager");
        role.setDescription("Can manage categoryies and conect the links.");
        secure.addRole(role);
    }
}
